/**
 * 系统公告列表
 *
 */
define(["app", "comm/pager/PagerControl", "js/services/MessageBox", "js/services/MuseService",
    "js/directives/DatetimePicker", 'js/controllers/feedback/FeedbackEditController'], function (app) {

    var deps = ["$scope", "$state", "$modal", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, MessageBox, MuseService) {
        //初始化查询对象 和 分页对象
        app.regsiterCacheData($scope, $state);
        $scope.queryObj = $scope.queryObj || {};
        $scope.pagerObj = $scope.pagerObj || {};

        //获取列表
        $scope.getObjList = function (pageNo, pageSize) {
            var curPage = pageNo || $scope.pagerObj.currentPage || 1;
            var param = {
                page: curPage, pageSize: pageSize || $scope.pagerObj.pageSize,
                param: {
                    title: 'like ' + ($scope.queryObj.title ? $scope.queryObj.title : ""),
                    hasRead: $scope.queryObj.hasRead ? $scope.queryObj.hasRead : "",
                    startTime: $scope.queryObj.startTime || "",
                    endTime: $scope.queryObj.endTime || ""
                }
            };

            MuseService.postObj(baseServices_restfulUrl + "feedback/search.json", param).success(
                function (data, status, headers, config) {
                    // console.log(data);
                    if (data.state == 0) {
                        var page = data.data;
                        $scope.feedbackList = page.data;

                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, page.total, curPage);
                    }
                })
            .error(function () {
                MessageBox.error('服务器正在开小差zZZ,获取不到数据');
            });
        };

        $scope.getObjList();

        //删除
        $scope.delete = function (feedback) {
            var dialog = MessageBox.confirm('确认要删除该信息吗？');
            dialog.result.then(function () {
                MuseService.post(baseServices_restfulUrl + 'feedback/delete.json', {id: feedback.id}).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '删除成功！', displayTime: 1000});
                            $scope.getObjList();
                        } else {
                            MessageBox.error('删除异常：' + data.message || "服务器正在开小差");
                        }
                    })
                .error(function () {
                    MessageBox.error('删除失败：服务器正在开小差');
                });
            });
        };

        //查看详情
        $scope.edit = function (obj) {
            $modal.open({
                templateUrl: 'views/feedback/edit.html',
                controller: 'FeedbackEditController',
                resolve: {
                    feedback: function () {
                        return angular.copy(obj);
                    }
                },
                size: 'md'
            }).result.then(function () {
                $scope.getObjList();
            }, function () {
                $scope.getObjList();
            });
        };

        $scope.goPassenger = function (passengerId) {
            $state.go('passengerEdit', {id: passengerId});
        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.getObjList($scope.pagerObj.currentPage, $scope.pagerObj.pageSize);
        }
    }

    controller.$inject = deps;
    app.lazy.controller("FeedbackListController", controller);
});
